package com.desire.test;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author desire
 */
public class _32_Thumbnailator的使用 {
    public static void main(String[] args) throws IOException {
        String src = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\bagua.png";
        String dst = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\bagua_thum.png";
        //1. 对图片进行指定尺寸的压缩 (缩略图)
        //Thumbnails.of(src).size(200, 200).toFile(dst);

        //2. 对图片按比例生成缩略图
        //Thumbnails.of(src).scale(.5).toFile(dst);

        //3. 旋转图片
        //Thumbnails.of(src).scale(1f).rotate(90).toFile(dst);

        //4. 格式转换
        // Thumbnails.of(src).scale(1f).outputFormat("gif").toFile(dst);

        //5. 剪裁
        //Thumbnails.of(src).scale(1.0).sourceRegion(0, 0, 100, 100).toFile(dst);

        //6. 水印图标
        var logo = ImageIO.read(new File("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\qr2.png"));
        //Thumbnails.of(src).scale(1.0).watermark(Positions.CENTER_RIGHT, logo, .5f).toFile(dst);

        //7. 水印文字
        BufferedImage image = new BufferedImage(100, 80, 2);
        Graphics2D g = image.createGraphics();
        g.setColor(Color.red);
        g.drawString("nihao ", 10, 35);
        g.dispose();
        Thumbnails.of(src).scale(1.0).watermark(Positions.CENTER, image, 1f).toFile("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\p_logo.jpg");

    }
}
